#include "logindialog.h"
#include "student.h"

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include <QVariant>
#include <QPalette>

LoginDialog::LoginDialog(QWidget *parent):QDialog(parent)
{
    this->resize(485,300);
    this->setWindowTitle("BUPT LMS Login");

    QPalette Pal(this->palette());

    Pal.setColor(QPalette::Window,Qt::white);
    this->setAutoFillBackground(true);
    this->setPalette(Pal);


    userLabel = new QLabel("Username",this);
    userLabel->move(55,65);

    userEditLine = new QLineEdit(this);
    userEditLine->move(120,63);
    userEditLine->resize(320,25);
    this->Username = userEditLine->text();

    pwdLabel = new QLabel("Password",this);
    pwdLabel->move(55,105);

    pwdEditLine = new QLineEdit(this);
    pwdEditLine->move(120,103);
    pwdEditLine->resize(320,25);
    this->Password = pwdEditLine->text();

    loginBtn = new MyPushButton(this);
    loginBtn->move(120,160);
    loginBtn->resize(150,25);
    loginBtn->setText("Sign in");

    exitBtn = new MyPushButton(this);
    exitBtn->move(290,160);
    exitBtn->resize(150,25);
    exitBtn->setText("Cancel");

    QLabel *aLine = new QLabel(this);
    aLine->setText("————————————————————————————————————————————————");
    aLine->resize(420,20);
    aLine->move(20,190);

    forgetLabel = new QLabel("I need help.",this);
    forgetLabel->move(40,230);
    forgetLabel->resize(180,30);
    forgetLabel->setAlignment(Qt::AlignRight);

    regLabel = new QLabel("New to BUPT LMS system?",this);
    regLabel->move(40,270);
    regLabel->resize(180,30);
    regLabel->setAlignment(Qt::AlignRight);

    forgetBtn = new MyPushButton(this);
    forgetBtn->move(240,220);
    forgetBtn->resize(200,30);
    forgetBtn->setText("Forget password?");
/*
    regBtn = new MyPushButton(this);
    regBtn->move(240,260);
    regBtn->resize(200,30);
    regBtn->setText("Create an account.");
*/
    regist();
    quit(exitBtn);

    connect(loginBtn,&QPushButton::clicked,this,[=](){
        if(this->db.loginCheck(this->userEditLine->text(),this->pwdEditLine->text()) == true){
            accept();
        }
    });
}

LoginDialog::~LoginDialog(){
}

void LoginDialog::quit(MyPushButton *ExitButton){
    connect(ExitButton,&QPushButton::clicked,this,[=](){
        if(this->isActiveWindow() == true){
            exit(0);
        }
    });
}

void LoginDialog::regDbNewAccount(){
    if(db.addItem(this->userEditLine->text(),this->pwdEditLine->text())){
        QWidget *window = new QWidget(this);
        window->showMaximized();
    }
    else{
        exit(4);
    }

}

void LoginDialog::regist(){
    /*
    Student Stu;
    Stu.setUserName(this->Username);
    Stu.setPassword(this->Password);
    */
    regBtn = new MyPushButton(this);
    regBtn->move(240,260);
    regBtn->resize(200,30);
    regBtn->setText("Create an account.");
    connect(regBtn,&QPushButton::clicked,this,&LoginDialog::regDbNewAccount);
}

void LoginDialog::regWindow(){
    QWidget *RegW = new QWidget(this);

}
